package com.hyk.commonLib.common.utils;

import android.util.TypedValue;
import android.widget.Toast;

import com.hyk.commonLib.R;

import es.dmoral.toasty.Toasty;

public class ToastUtil {
    private static ToastUtil instance;

    private Toast toast;

    public static ToastUtil getSingleton() {
        if (instance == null) {
            synchronized (ToastUtil.class) {
                if (instance == null) {
                    instance = new ToastUtil();
                }
            }
        }
        return instance;
    }

    private ToastUtil() {
        TypedValue typedValue = new TypedValue();
        ResUtils.getResource().getValue(R.dimen.text_size_m, typedValue, false);
        int textSizeX = (int) TypedValue.complexToFloat(typedValue.data);
        Toasty.Config.getInstance()
                .setTextSize(textSizeX)
                .apply();
    }

    public void showNormal(CharSequence str) {
        AppUtils.runOnUI(() -> {
            cancel();
            toast = Toasty.normal(AppUtils.getAppContext(), str);
            toast.show();
        });
    }

    public void showSuccess(CharSequence str) {
        AppUtils.runOnUI(() -> {
            cancel();
            toast = Toasty.success(AppUtils.getAppContext(), str);
            toast.show();
        });
    }

    public void showError(CharSequence str) {
        AppUtils.runOnUI(() -> {
            cancel();
            toast = Toasty.error(AppUtils.getAppContext(), str);
            toast.show();
        });
    }

    public void showWarning(CharSequence str) {
        AppUtils.runOnUI(() -> {
            cancel();
            toast = Toasty.warning(AppUtils.getAppContext(), str);
            toast.show();
        });
    }

    public void showInfo(CharSequence str) {
        AppUtils.runOnUI(() -> {
            cancel();
            toast = Toasty.info(AppUtils.getAppContext(), str);
            toast.show();
        });
    }

    public void cancel() {
        if (toast != null) {
            toast.cancel();
        }
    }

    public void showCommonRetryError() {
        showError("发生错误，请稍后重试~");
    }

    public void showLinkServerError() {
        showError("网络连接异常，请稍后重试~");
    }

    public void showDataMissed() {
        showError("数据缺失");
    }

    public void showPermissionRejectError() {
        showError("未能成功获取权限，请重试~");
    }
}